package com.my.mynote;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.my.bean.Note;
import com.my.helper.NoteDbOpenHelper;
import com.my.util.ToastUtil;

import java.text.SimpleDateFormat;
import java.util.Date;

public class AddActivity extends AppCompatActivity {
    private EditText et_title,et_content;
    private NoteDbOpenHelper mNoteDbOpenHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        initView();
        mNoteDbOpenHelper = new NoteDbOpenHelper(this);
    }

    private void initView() {
        et_title = findViewById(R.id.et_title);
        et_content = findViewById(R.id.et_content);
    }

    public String getCurrentTimeFormat(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd HH:mm:ss");
        Date date = new Date();
        return simpleDateFormat.format(date);
    }

    public void add(View view) {
        String content = et_content.getText().toString().trim();
        String title = et_title.getText().toString().trim();
        if (TextUtils.isEmpty(title)){
            ToastUtil.showShortToast(this,"标题不能为空");
            return;
        }
        Note note = new Note();
        note.setTitle(title);
        note.setContent(content);
        note.setCreate(getCurrentTimeFormat());
        Long row = mNoteDbOpenHelper.insertData(note);
        if (row!=-1){
            ToastUtil.showLongToast(this,"添加成功！");
            //添加成功之后要自动返回到列表界面
            this.finish();
        }else {
            ToastUtil.showLongToast(this,"添加失败！");
        }
    }


}